package com.ms.erpBase;

import java.util.List;

import javax.servlet.http.HttpSession;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.ms.common.model.BusEmploee;
import com.ms.common.model.User;
import com.ms.common.util.MsUtility;

public class BusEmploeeController extends Controller {

	public void createOrUpdate(){
	
		BusEmploee be=getBean(BusEmploee.class);
		if(be.getId()==null){
			be.save();
		}else{
			be.update();
		}
		renderJsp("/jsp/busEmploee/list.jsp");
	}
	
	
	public void list(){
		BusEmploee be=getBean(BusEmploee.class);
		Page<BusEmploee> page = BusEmploee.dao.findPageConditions(getBean(BusEmploee.class),getParaToInt("page"),getParaToInt("rows") );
		renderJson(MsUtility.TransToEasyUiPage(page));
		
	}
	
	public void edit(){
		String id=getPara("id");
		BusEmploee be=BusEmploee.dao.findById(id);
		setAttr("busEmploee", be);
		renderJsp("/jsp/busEmploee/form.jsp");
	}
	
	public void beginCreate(){
		renderJsp("/jsp/busEmploee/form.jsp");
	}
	
	public void delete(){
		String ids=getPara("items");
		String[] idarr=ids.split(",");
		for (String id : idarr) {
			BusEmploee.dao.deleteById(id);
		}
		
	}
	
	
	/**
	 * 查询所有用于人员的选择
	 */
	public void findAll(){
		renderJson(BusEmploee.dao.find("SELECT * FROM  bus_emploee"));
	}
	
	public void login(){
		
		String userName=getPara("userName");
		String password=getPara("password");
		List<User> user=User.dao.find("SELECT * FROM USER WHERE USER_NAME='"+userName+"'");
		if(user!=null&&user.size()>0){
			if(password.equals(user.get(0).getPassword())||user.get(0).getPassword()==password){
				HttpSession session=getSession();
				session.setAttribute("userId", user.get(0).getId());
				session.setAttribute("userName", user.get(0).getFullName());
				if("1".equals(user.get(0).getUserType())){
					renderJsp("/main.jsp");
				}else if("2".equals(user.get(0).getUserType())){
					renderJsp("/out_main.jsp");
				}else{
					renderJsp("/out_guest_main.jsp");
				}
			}else{
				setAttr("msg", "用户名密码错误！");
				renderJsp("/login.jsp");
			}
		}else{
			setAttr("msg", "不存在该用户！");
			renderJsp("/login.jsp");
		}
	}
	
}
